package com.global.college.ui.activity;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


import com.global.college.R;
import com.global.college.base.BaseActivity;

import java.lang.reflect.InvocationTargetException;

import butterknife.OnClick;

//浏览器
public class WebViewActivity extends BaseActivity {

    private WebView mWebView;
    private ProgressBar progressbar;
    private String strurl = "";
    private MyTimer mTimer;
    private int progress = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_web;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(true);
        setShowBack(true);
        setTitle(getIntent().getStringExtra("title"));
        mWebView = (WebView) findViewById(R.id.mwebview);
        progressbar = (ProgressBar) findViewById(R.id.progressbar);

        String strurl = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(strurl)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
                mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
//            WebSettings ws = mWebView.getSettings();
//            ws.setAppCacheEnabled(true);//启用localstorage本地存储api
//            ws.setLightTouchEnabled(true);//启用选中功能
//            ws.setDomStorageEnabled(true);//启用dom存储(关键就是这句)，貌似网上twitter显示有问题也是这个属性没有设置的原因
//            ws.setDatabaseEnabled(true);//启用html5数据库功能
//            ws.setTextSize(WebSettings.TextSize.NORMAL);//通过设置WebSettings，改变HTML中文字的大小 (使用后字体变大)
//            ws.setJavaScriptEnabled(true);//设置js可用
//            //  WebView中Http和Https混合问题
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//            }


            mWebView.getSettings().setBlockNetworkImage(false);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.getSettings().setJavaScriptEnabled(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }

            mWebView.setWebViewClient(new WeiboWebViewClient());
            mWebView.setWebChromeClient(new WebChromeClient());
            mWebView.loadUrl(strurl);
            //mWebView.loadDataWithBaseURL(strurl);
        }
    }

    @Override
    public void getInitData() {

    }


    @Override
    public void onResume() {
        super.onResume();
        try {
            mWebView.getClass().getMethod("onResume")
                    .invoke(mWebView, (Object[]) null);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        try {
            mWebView.getClass().getMethod("onPause")
                    .invoke(mWebView, (Object[]) null);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @OnClick({})
    public void onViewClicked(View view) {
        switch (view.getId()) {

        }
    }

    private class WeiboWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (mTimer == null) {
                mTimer = new MyTimer(15000, 50);
            }
            mTimer.start();
            progressbar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
//            if (mTimer != null) {
//                mTimer.cancel();
//            }
            progress = 0;
            progressbar.setProgress(100);
            progressbar.setVisibility(View.GONE);
        }
    }

    /* 定义一个倒计时的内部类 */
    private class MyTimer extends CountDownTimer {
        public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            progress = 100;
            progressbar.setVisibility(View.GONE);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (progress == 100) {
                progressbar.setVisibility(View.GONE);
            } else {
                progressbar.setProgress(progress++);
            }
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebView.canGoBack())
                mWebView.goBack();
            else
                finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
